/**
 * 描述: 渠道标示列表页
 *
 * @author jiafei
 * @since 2021/12/06 11:23
 */
layui.extend({
    zmnExportUtil: '{/}/static/js/zmn.excel.util'
}).define(['index', 'form', 'table', 'laypage', 'layedit', 'zmnExportUtil'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laytpl = layui.laytpl;
    let zmnExportUtil = layui.zmnExportUtil;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: ['exports'],
        page: true,
        limit: json.pageSize,
        height: 'full-149',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        }
    });
    laypage.render(layui.$.extend({limits: [10, 20, 30, 40, 50, 100]}, zmnPage.laypageOptions(json)));

    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.channelId);
    });


    $(document).on("click", "#delChannelAssMark", function (e) {
        let channelId = $(this).attr("data-id");
        delChannel(new Array(channelId));
    })

    function delChannel(ids) {
        if (ids.length === 0) {
            return layer.alert("请选择至少1条数据！");
        }
        layer.confirm('解除关联后，将会从列表移除，请确认是否解除关联?', {icon: 3, title: '解除关联提示'}, function (index) {
            //do something
            zmnPage.ajaxSimple("/channel/mark/modify/group/assChannels/update.action", {
                delIds: ids.join(","),
                markId: json.vo.markId
            }, 1000);
            layer.close(index);
        });
    }

    // 启用/停用/新增
    table.on("toolbar(zmn-table)", function (obj) {

        //导出excel
        if (obj.event === 'CUSTOM_EXPORT') {
            let index = layer.load(1, {
                shade: [0.5, '#DBDBDB']
            });
            zmnUtil.downloadLimitByConfig("/channel/mark/group/outputExcel.action?" + $("#zmn-query").serialize(), "关联渠道管理.xlsx", function () {
                layer.close(index);
            });
            return false;
        }

        //启用停用
        if (obj.event === 'delChannel'){
            delChannel(ids);
        }

        if (obj.event === "updateMark") {
            openUpdateMarkSub();
        }
    });

    let subIndex

    function openUpdateMarkSub() {
        subIndex = layer.open({
            type: 1,
            title: "标示调整",
            area: ['610px', '350px'],
            content: $("#inOutHtml"),
        });
        $("#inOutHtml").show();
    }

    $(document).on("click", "#markSubCancel", function (e) {
        layer.close(subIndex);
    })

    let updateChannelId;
    let batchOperate = false;
    $(document).on('click', '#updateChannelAssMark', function (e) {
        updateChannelId = $(this).attr("data-id");
        batchOperate = true;
        openUpdateMarkSub();
    })


    form.on('submit(zmn-form-reset)', function (obj) {
        form.val("zmn-query", {
            "keyword": "" // "name": "value"
        });
        return false;
    });

    // $(document).on('click', '#zmn-form-mark-update', function () {
    //     let val = $('#newMarkId').val();
    //     console.log(val)
    // })

    form.on('submit(zmn-form-mark-update)', function (obj) {
        let data = obj.field;
        data.newMarkId = Number(data.newMarkId);
        data.oldMarkId = Number(data.oldMarkId);

        if (data.oldMarkId === data.newMarkId) {
            layer.alert("请勿选择相同标识！");
            return false;
        }

        if (batchOperate) {
            data.channelIds = new Array(updateChannelId);
        } else {
            if (ids.length === 0) {
                layer.alert("请选择至少1条数据！");
                return false;
            }
            data.channelIds = ids;
        }

        var url = "/channel/mark/modify/group/updateChannelAssMark.action";


        var $ = layui.$;
        $.ajax({
            type: "POST", url: url,
            dataType: "json", data: {oldMarkId: Number(data.oldMarkId), newMarkId: Number(data.newMarkId), channelIds: data.channelIds.join(",")},
            success: function (json) {
                if (json.status == 200) {
                    zmnPage.reload(json.message, timeout);
                } else {
                    layer.msg(json.message);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });


    });

    //对外暴露的接口
    exports('plat_channel_mark_group_list', {});
});

